package com.commerceerp.system.service.Impl;


import com.commerceerp.system.entity.OrderInfo;
import com.commerceerp.system.mapper.OrderMapper;
import com.commerceerp.system.service.OrderService;
import com.commerceerp.system.util.ApiResult;
import com.commerceerp.system.util.DateUtil;
import com.commerceerp.system.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;



@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;


    /**
     * 获取所有订单信息列表
     */
    @Override
    public List<Map> getAllOrderInfo(String userId, String orderChannel, String storeName, String orderId, String addressCountry, String orderStatus, Integer orderDownTimePX, Integer orderSkuPX, Integer orderMoneyPX,String beginTime,String endTime) {

        List<Map> orderInfoList = orderMapper.getAllOrderInfo(userId,orderChannel,storeName,orderId,addressCountry,orderStatus,orderDownTimePX,orderSkuPX,orderMoneyPX,beginTime,endTime);

        for(Map<String,Object> order : orderInfoList){
            //根据订单id获取订单里面的商品信息
            List<Map> orderGoodInfo = orderMapper.getGoodInfosByOrderId(order.get("orderId").toString());

            order.put("orderGoodInfo",orderGoodInfo);
        }

        return orderInfoList;
    }

    /**
     * 更改订单状态
     */
    @Override
    public Integer updOrderStatus(String orderId, String orderStatus) {

        return orderMapper.updOrderStatus(orderId,orderStatus,DateUtil.getYMDHMSDate());
    }

    /**
     * 根据订单号获取详情
     */
    @Override
    public Map<String, Object> getOrderDetailInfo(String orderId) {

        Map<String,Object> orderDetail = orderMapper.getOrderDetailInfo(orderId);
        if(orderDetail != null){
            //根据订单id获取订单里面的订单详情和商品信息
            List<Map> orderGoodInfo = orderMapper.getGoodInfosByOrderId(orderId);
            orderDetail.put("orderGoodInfo",orderGoodInfo);
            if(!orderGoodInfo.isEmpty()){
                Boolean flag = false;
                StringBuffer str = new StringBuffer();
                for(Map<String,Object> goodInfo : orderGoodInfo){
                    if(flag){
                        str.append(",");
                        str.append(goodInfo.get("orderSku").toString());
                    }else{
                        str.append(goodInfo.get("orderSku").toString());
                        flag =true;
                    }
                }
                Map<String,Object> orderGoodMoneyAndCount = this.orderMapper.getGoodInfoByOrderDetail(orderDetail.get("orderId").toString());
                orderDetail.put("orderSku",str.toString());
                orderDetail.put("orderGoodPrice",orderGoodMoneyAndCount.get("orderGoodPrice") == null ? 0 : orderGoodMoneyAndCount.get("orderGoodPrice").toString());
                orderDetail.put("orderGoodNumber",orderGoodMoneyAndCount.get("orderGoodNumber") == null ? 0 : orderGoodMoneyAndCount.get("orderGoodNumber").toString());
            }else{
                orderDetail.put("orderSku","");
                orderDetail.put("orderGoodPrice","0");
                orderDetail.put("orderGoodNumber","0");
            }
        }
        return orderDetail;
    }

    /**
     * 更改收货信息
     */
    @Override
    public Integer updOrderAddressInfo(OrderInfo orderInfo) {

        Integer success = orderMapper.updOrderAddressInfo(orderInfo);

        return success;
    }

    //订单状态统计
    @Override
    public ApiResult orderCount(String userId) {

        return new ApiResult(orderMapper.orderCount(userId));
    }


    /**
     * 新增订单物流信息
     */
    @Override
    public Integer saveLogisticInfoByOrderId(String logisticName, String logisticPhone, String logisticInfo, String logisticRemark, String logisticOrderId, String logisticStatus) {

        Integer success = orderMapper.saveLogisticInfoByOrderId(StringUtils.getUUID(),logisticName,logisticPhone,logisticInfo,logisticRemark,logisticOrderId,DateUtil.getYMDHMSDate(),logisticStatus);

        return success;
    }
}
